package com.android.siwinn.jcy;

import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.siwinn.jcy.viewpull.PullToRefreshBase.OnRefreshListener;
import com.android.siwinn.jcy.viewpull.PullToRefreshListView;

public class Images extends Activity {
    static final int MENU_MANUAL_REFRESH = 0;
    static final int MENU_DISABLE_SCROLL = 1;

    private PullToRefreshListView mPullRefreshListView;
    private ImageAdapter mAdapter;
    
    LayoutInflater factory;
    
    private static final int Image_count = 8;
    public String iamge_interface = "http://3ghbjc.xicp.net/webserver/server.ashx/GetImagArticleList?code=5666DFA2C1D11E39&index=0&count=" + Image_count;
    
    List<ImageArticleInfo> imageArticleInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_to_refresh_list);
        
        new GetDataTask().execute();
        
        factory = getLayoutInflater();

        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

        // Set a listener to be invoked when the list should be refreshed.
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });
        ImageView mTopImageView = (ImageView) this.findViewById(R.id.lv_backtotop);
        mPullRefreshListView.setBackToTopView(mTopImageView);
        ListView actualListView = mPullRefreshListView.getRefreshableView();

        mAdapter = new ImageAdapter();
        
        // You can also just use setListAdapter(mAdapter)
        actualListView.setAdapter(mAdapter);
    }
    
    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            String xml = XmlParser.getXml(iamge_interface);
            imageArticleInfo = XmlParser.getImageArticleInfo(xml);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mAdapter.notifyDataSetChanged();
            mPullRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
    
    class ImageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
        	if(imageArticleInfo == null) {
        		return 0;
        	} else {
        		return imageArticleInfo.size();
        	}
        }

        @Override
        public Object getItem(int position) {
            return imageArticleInfo.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null) {
                convertView = factory.inflate(R.layout.image_list_item, null);
                holder = new ViewHolder();
                holder.image_subject = (TextView) convertView.findViewById(R.id.image_subject);
                holder.first_image = (ImageView) convertView.findViewById(R.id.first_image);
                holder.second_image = (ImageView) convertView.findViewById(R.id.second_image);
                holder.image_num = (TextView) convertView.findViewById(R.id.image_num);
                holder.image_id = (TextView) convertView.findViewById(R.id.image_id);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.image_subject.setText(imageArticleInfo.get(position).getArticleTitle());
            holder.image_num.setText(imageArticleInfo.get(position).getImgCount());
            holder.image_id.setText(imageArticleInfo.get(position).getId());
            new GetImageAsync().execute(holder.first_image,holder.second_image,imageArticleInfo.get(position).getImgUrl1(),imageArticleInfo.get(position).getImgUrl2());
            return convertView;
        }
        
        class ViewHolder {
            TextView image_subject;
            ImageView first_image;
            ImageView second_image;
            TextView image_num;
            TextView image_id;
        }
        
        class GetImageAsync extends AsyncTask<Object, Void, Bitmap[]> {
        	ImageView image1;
        	ImageView image2;
        	String url1;
        	String url2;
        	Bitmap[] bits = new Bitmap[2];
			@Override
			protected Bitmap[] doInBackground(Object... arg0) {
				image1 = (ImageView) arg0[0];
				image2 = (ImageView) arg0[1];
				url1 = (String) arg0[2];
				url2 = (String) arg0[3];
				InputStream is1 = XmlParser.getXMLStream(url1);
				InputStream is2 = XmlParser.getXMLStream(url2);
				BitmapFactory.Options option = new BitmapFactory.Options();
				option.inSampleSize = 3;
				if(is1 != null) {
	              Bitmap bitmap1 = BitmapFactory.decodeStream(is1, null, option);
	              bits[0] = bitmap1;
				}
				
				if(is2 != null) {
					Bitmap bitmap2 = BitmapFactory.decodeStream(is2, null, option);
					bits[1] = bitmap2;
				}
				return bits;
			}

			@Override
			protected void onPostExecute(Bitmap[] result) {
				image1.setImageBitmap(result[0]);
				image2.setImageBitmap(result[1]);
				super.onPostExecute(result);
			}
        	
        }
    }

}
